﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            Worker();
        }

        protected override void OnStop()
        {
        }

        public void Worker()
        {
            StreamWriter sw = new StreamWriter("c:\\Temp\\Log.txt", true);
            try
            {

                sw.WriteLine(String.Format("Started at {0}.",DateTime.Now));

                string CmdText = "/C c:\\Temp\\ServiceTest.cmd";
                System.Diagnostics.Process.Start("cmd.exe", CmdText);

                sw.WriteLine(String.Format("Completed at {0}.", DateTime.Now));
            }
            catch(Exception ex)
            {
                sw.WriteLine(ex.Message);

                sw.Close();
            }

            sw.Close();
        }
    }
}
